package com.javaoop.couese;

/**
 * 课程：JavaOOP ，山西应用科技学院信息工程学院计算机科学与技术专业 2201班
 *
 * @author by 2022010101张三
 * &#064;user  Administrator
 * @Date 2024/12/5 20:25
 * @Description 任务或用途 TODO: 定义点的属性和行为，实现刻画接口
 */
public class Point implements Drawable {
    private int x;
    private int y;
    private char c;
    public Point(int x, int y, char c) {
        this.x = x;
        this.y = y;
        this.c = c;
    }

    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public char getC() {
        return c;
    }
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }
    public void setC(char c) {
        this.c = c;
    }

    @Override
    public String toString() {
        return "Point{" + "x=" + x +
                ", y=" + y + ", c=" + c + '}';
    }

    /**
     *
     * @param x 坐标
     * @param y 坐标
     * @param c 点字符
     */
    public void setPoint(int x, int y, char c) {
        this.x = x;
        this.y = y;
        this.c = c;
    }

    /**
     *
     * @param cv 画布
     */

    @Override
    public void draw(Canvas cv) {
        cv.getMatrix()[this.y][this.x]=this.c;
    }

    /**
     *
     * @param args 入口参数
     */
    public static void main(String[] args) {
        System.out.println("\n============ 画点 ============\n");
        Canvas cv=new Canvas(20,40,'.');
        Point p=new Point(10,10,'X');
        p.draw(cv);
        p.x=9;  //私有成员自己的对象可以直接访问
        cv.view();
        System.out.println("========= 画多点 使用封装==========");
        for (int i = 0; i < 20; i++) {
                p.setY(19-i);
                p.setX(i*2);
                p.setC('=');
                p.draw(cv);
        }
        cv.view();
        System.out.println("========= 画多点 使用setPoint ==========");
        for (int i = 0; i < 20; i++) {
            p.setPoint(i,i,'@');
            p.draw(cv);
        }
        cv.view();

        System.out.println("========== 画随机点 ==========");
        for (int i = 0; i < 20; i++) {
            p.setY( (int) (Math.random()*20)  );
            p.setX( (int) (Math.random()*40)  );
            p.setC('#');
            p.draw(cv);
        }
        cv.view();
        System.out.println("========== 画圆 ==========");
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if((i-10)*(i-10)+(j-10)*(j-10)<=50) {
                    p.setY(i);
                    p.setX(j);
                    p.setC('o');
                    p.draw(cv);
                }
            }
        }
        cv.view();  //显示画图结果

        for (int i = 0; i < 20; i++) {
            System.out.printf("i=%d, j=%d\n",(int) (Math.random()*40), (int) (Math.random()*20));
            new Point((int) (Math.random()*40), (int) (Math.random()*20),'c').draw(cv);
        }
        cv.view();

    }
}
